package ma.meditel.reporting.dao.implementations;

import java.util.Date;
import java.util.List;

import ma.meditel.reporting.dao.interfaces.ITrafficPerClassDao;
import ma.meditel.reporting.entities.TrafficClassification;
import ma.meditel.reporting.util.HibernateUtil;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

public class TrafficPerClassDaoImpl implements ITrafficPerClassDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<TrafficClassification> getAllInterfaceTrafficsBetween(Long idInterface, Date startDate, Date endDate, Long classTraffic) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			
			Criteria cr = session.createCriteria(TrafficClassification.class);
			cr.add(Restrictions.ge("date", startDate))
			.add(Restrictions.le("date", endDate))
			.add(Restrictions.eq("tclasse.refTClasse", classTraffic))
			.add(Restrictions.eq("iinterface.idInterface", idInterface))
			.addOrder(Order.asc("date"));
			
			tx.commit();
			
			return cr.list();
		
		} catch (Exception e) {
			if (tx != null) tx.rollback();
			throw e;
			
		} finally {
			session.close();
		}
	}

}
